﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObject;
using Model;

namespace ViewLayer.Pages.Epics
{
    public partial class InicioEpic : System.Web.UI.Page
    {
        User user;

        protected void Page_Load(object sender, EventArgs e)
        {
            user = (User)Session["User"];
            if (user == null)
            {
                Response.Redirect("../Account/Login.aspx");
            }

            if (!IsPostBack)
            {
                if (Session["Project"] != null)
                {
                    this.populateDDLRelease(this.ddlReleases);
                    this.populateEpic();
                }
            }
        }

        protected void lnkbImportEpic_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Epics/Importacao.aspx");
        }

        protected void lnkbCadastro_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Epics/CadastroEpic.aspx");
        }

        private void populateDDLRelease(DropDownList ddList)
        {
            ddList.DataSource = ReleaseBO.Instance.GetReleaseByProject(Session["Project"] as Project);
            ddList.DataTextField = "Nome";
            ddList.DataValueField = "Id";
            ddList.DataBind();

            ddList.Items.Add(new ListItem("Sem Associação", "0"));

            ddList.SelectedValue = "0";
        }

        private void populateEpic()
        {
            //consulta todas releases do projeto.
            if (int.Parse(this.ddlReleases.SelectedValue) == 0)
            {
                this.grdEpics.DataSource = EpicBO.Instance.GetEpicByProject(Session["Project"] as Project);
            }
            else
            {
                this.grdEpics.DataSource = EpicBO.Instance.GetEpicByRelease(ReleaseBO.Instance.GetById(int.Parse(this.ddlReleases.SelectedValue)));
            }

            this.grdEpics.DataBind();
        }

        protected void grdEpics_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Epic epic = e.Row.DataItem as Epic;

                //exibição
                (e.Row.FindControl("lblNomeEpic") as Label).Text = epic.Nome;
                (e.Row.FindControl("lblDescEpic") as Label).Text = epic.Descricao;
                (e.Row.FindControl("lblMelhorCaso") as Label).Text = epic.MelhorCaso.ToString();
                (e.Row.FindControl("lblCasoMedio") as Label).Text = epic.CasoMedio.ToString();
                (e.Row.FindControl("lblPiorCaso") as Label).Text = epic.PiorCaso.ToString();
                (e.Row.FindControl("btnAtualizar") as Button).CommandArgument = epic.Id.ToString();

                //edição
                (e.Row.FindControl("txtNomeEpic") as TextBox).Text = epic.Nome;
                (e.Row.FindControl("txtDescEpic") as TextBox).Text = epic.Descricao;
                (e.Row.FindControl("txtMelhorCaso") as TextBox).Text = epic.MelhorCaso.ToString();
                (e.Row.FindControl("txtCasoMedio") as TextBox).Text = epic.CasoMedio.ToString();
                (e.Row.FindControl("txtPiorCaso") as TextBox).Text = epic.PiorCaso.ToString();

                (e.Row.FindControl("btnSalvar") as Button).CommandArgument = epic.Id.ToString();

                DropDownList ddlReleasesToAssociate = e.Row.FindControl("ddlReleasesToAssociate") as DropDownList;

                this.populateDDLRelease(ddlReleasesToAssociate);

                //atualiza o valor da release associada a epic
                if (epic.Release != null)
                {
                    ddlReleasesToAssociate.SelectedIndex = epic.Release.Id;
                    (e.Row.FindControl("lblReleaseAssociate") as Label).Text = epic.Release.Nome;
                }
            }
        }

        protected void grdEpics_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.grdEpics.PageIndex = e.NewPageIndex;
            this.populateEpic();
        }

        protected void ddlReleases_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.populateEpic();
        }

        protected void grdEpics_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "editar")
            {
                GridViewRow grd = (GridViewRow)((Control)e.CommandSource).NamingContainer;
                ControlsVisibilityManager(grd, true);               
            }

            if (e.CommandName.ToLower() == "salvar")
            {
                GridViewRow grd = (GridViewRow)((Control)e.CommandSource).NamingContainer;
                Epic epic = EpicBO.Instance.GetById(Convert.ToInt32(e.CommandArgument));

                epic.Nome = (grd.FindControl("txtNomeEpic") as TextBox).Text;
                epic.Descricao = (grd.FindControl("txtDescEpic") as TextBox).Text;
                epic.MelhorCaso = Convert.ToDouble((grd.FindControl("txtMelhorCaso") as TextBox).Text);
                epic.CasoMedio = Convert.ToDouble((grd.FindControl("txtCasoMedio") as TextBox).Text);
                epic.PiorCaso = Convert.ToDouble((grd.FindControl("txtPiorCaso") as TextBox).Text);

                var relIndex = Convert.ToInt32((grd.FindControl("ddlReleasesToAssociate") as DropDownList).SelectedValue);

                //release to associate
                if (relIndex > 0)
                {
                    epic.Release = ReleaseBO.Instance.GetById(relIndex);
                }
                else
                {
                    epic.Release = null;
                }

                EpicBO.Instance.Update(epic);

                populateEpic();
                ControlsVisibilityManager(grd, false);
            }
        }

        /// <summary>
        /// Gerencia os controles dos campos na página.
        /// </summary>
        /// <param name="grd"></param>
        /// <param name="editar"></param>
        private static void ControlsVisibilityManager(GridViewRow grd, bool editar)
        {
            (grd.FindControl("txtNomeEpic") as TextBox).Visible = editar;
            (grd.FindControl("txtDescEpic") as TextBox).Visible = editar;
            (grd.FindControl("txtMelhorCaso") as TextBox).Visible = editar;
            (grd.FindControl("txtCasoMedio") as TextBox).Visible = editar;
            (grd.FindControl("txtPiorCaso") as TextBox).Visible = editar;            
            (grd.FindControl("btnSalvar") as Button).Visible = editar;
            (grd.FindControl("ddlReleasesToAssociate") as DropDownList).Visible = editar;

            (grd.FindControl("lblNomeEpic") as Label).Visible = !editar;
            (grd.FindControl("lblDescEpic") as Label).Visible = !editar;
            (grd.FindControl("lblMelhorCaso") as Label).Visible = !editar;
            (grd.FindControl("lblCasoMedio") as Label).Visible = !editar;
            (grd.FindControl("lblPiorCaso") as Label).Visible = !editar;
            (grd.FindControl("btnAtualizar") as Button).Visible = !editar;
            (grd.FindControl("lblReleaseAssociate") as Label).Visible = !editar;
        }    
    }
}